const fp= require('lodash/fp')
//数据
const cars =[
    {name:'car1',horsepower:660,dollar_value:7000,instock:10000},
    {name:'car2',horsepower:500,dollar_value:5000,instock:20000},
    {name:'car3',horsepower:360,dollar_value:3000,instock:30000},
    {name:'car4',horsepower:660,dollar_value:1000,instock:40000}
]
// 1
//获取最后一条数据
let getLastCar =cars=>fp.last(cars);
//获取库存数据
let getInstock = value => fp.prop('instock',value);

//调用函数组合,获取最后一条汽车的库存
let lastInstock =fp.flowRight(getInstock,getLastCar);
console.log(lastInstock(cars));

//2
//获取最后一条数据
let getFirstCar =cars=>fp.first(cars);
//获取汽车名称
let getCarName = value => fp.prop('name',value);
//调用函数组合,获取第一条汽车的名称
let firstCarName =fp.flowRight(getCarName,getFirstCar);
console.log(firstCarName(cars));

// 3 
let _average =function(xs){
    //console.log("xs",xs);
    //console.log("reduce",fp.reduce(fp.add,0,xs))
    return fp.reduce(fp.add,0,xs) / xs.length
}

let averageDollarValue = (cars)=>{
    return fp.map(car => car.dollar_value,cars)
}
let dollar = fp.flowRight(_average,averageDollarValue);
console.log("平均价格：",dollar(cars));
//4
let toLower =str =>  fp.toLower(str)
let _underscore = fp.replace(/\W+/g,'_')
let toArray = value => {
    let array =[];
    array.push(value)
    return array;
}  ;
//let map = str=> fp.map(toLower, str);
// const log = fp.curry((tag,v)=>{
//     console.log(tag,v);
//     return v;
// })
let sanitiseName =fp.flowRight(toArray, _underscore,toLower);
console.log(sanitiseName(["Hello World"]));
